package cool.zhoujie.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import cool.zhoujie.MyApplication;
import cool.zhoujie.R;
import cool.zhoujie.db.RecentPlay;

public class RecentAdapter extends ArrayAdapter<RecentPlay> {

    private int resourceId;

    public RecentAdapter(@NonNull Context context, int resource, @NonNull List<RecentPlay> objects) {
        super(context, resource, objects);
        this.resourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        RecentPlay rec = getItem(position);
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
        } else {
            view = convertView;
        }
        ImageView songPic = view.findViewById(R.id.item_songPic);
        TextView songName = view.findViewById(R.id.item_songName);
        TextView artistName = view.findViewById(R.id.item_artistName);
        //圆角化
        Glide.with(MyApplication.getContext())
                .load(rec.songPic)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                .into(songPic);
        songName.setText(rec.songName);
        artistName.setText(rec.artistName);
        return view;
    }
}
